package iso3.pt.action;



import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

import iso3.pt.model.Alumno;
import iso3.pt.model.Profesor;
import iso3.pt.service.PtDaoService;



/**
 * <p> Validate a user login. </p>
 */
public class LoginAction  extends ActionSupport implements Preparable{
	
	private String username = null;
	private String password = null;
	private String select = null;
	private PtDaoService dao;
	
	public String Login() throws Exception {
		return SUCCESS;
	}

	public String doLogin() throws Exception {
		System.out.println("Validating login");
		System.out.println(getSelect());
		
		Map s = ActionContext.getContext().getSession();
		if (getUsername().equals("") && getPassword().equals("")){
			return INPUT;
		}else{
			if (getSelect().equals(getText("label.login.alumno"))){
				try {
					Alumno alu = dao.loginAlumno(Integer.valueOf(getUsername()), getPassword());
					s.put("logged", alu);
					return "listStudentSubjects";
				} catch (Exception e) {
					return ERROR;
				}			
			}else if (getSelect().equals(getText("label.login.profesor"))){
				try {
					Profesor prof = dao.loginProfesor(Integer.valueOf(getUsername()), getPassword());
					s.put("logged", prof);
					return "listLecturerSubjects";
				} catch (Exception e) {
					return ERROR;
				}
			}
		}
		return INPUT;
	}
	
	public String doLogout() throws Exception {
		ActionContext.getContext().getSession().clear();
		return INPUT;
	}

    public String getUsername() {
        return username;
    }

    public void setUsername(String value) {
        username = value;
    }

    public String getPassword() {
    	return password;
    }

    public void setPassword(String value) {
    	password = value;
    }

    public String getSelect() {
    	return select;
    }

    public void setSelect(String value) {
        select = value;
    }

	@Override
	public void prepare() throws Exception {	
			dao = new PtDaoService();
	}
}
